//
//  CameraViewController.m
//  HuaTaiH5
//
//  Created by 李挺哲 on 16/1/14.
//  Copyright © 2016年 ahy. All rights reserved.
//

#import "CameraViewController.h"
#import "LeafButton.h"
#import "CameraManger.h"
#import <MediaPlayer/MediaPlayer.h>
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
@interface CameraViewController ()<CameraMangerDelegate>
{
    CameraManger * _camera;
    UIImageView *showImageView;
    LeafButton *takeButton;
    MPMoviePlayerController *playerView;
    UIView *  containerView;
}
@property (weak, nonatomic) IBOutlet UIButton *reTakeButton;
@property (weak, nonatomic) IBOutlet UIButton *confirmButton;

@end

@implementation CameraViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _camera=[[CameraManger alloc] init];
    _camera.cameraDelegate=self;
    takeButton = [[LeafButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    takeButton.center=CGPointMake(ScreenWidth/2, ScreenHeight-25);
    takeButton.tag = 100;
    [self.view addSubview:takeButton];

    _reTakeButton.hidden=YES;
    _confirmButton.hidden=YES;
    
       containerView=[[UIView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-50-64)];
    
    containerView.backgroundColor=[UIColor clearColor];
    [self.view addSubview:containerView];
    
    showImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-50-64)];
    showImageView.hidden=YES;
    [containerView addSubview:showImageView];
    playerView=[[MPMoviePlayerController alloc] init];
    playerView.view.frame=CGRectMake(0, 64, ScreenWidth, ScreenHeight-50-64);
    playerView.view.hidden=YES;
    [self.view addSubview:playerView.view];

    if(_taskCode==1){
        
        __block CameraManger *blockCamera = _camera;
        [takeButton setClickedBlock:^(LeafButton *button) {
            NSLog(@"我被点中了");
            
            [blockCamera captureStillImage];
        }];

    }else{
    
       
        takeButton.type=LeafButtonTypeVideo;
        
        __block CameraManger *blockCamera = _camera;
        [takeButton setClickedBlock:^(LeafButton *button) {
            NSLog(@" record我被点中了");
            [blockCamera  videoRecord];
        }];

    }
    if (_taskCode==1) {
        [_camera setupCaptureImageManager:RearFacingCamera];
           }else{
        [_camera setupCaptureVideoManager:RearFacingCamera];
       
    }
  
    CALayer *layer=containerView.layer;
    CGRect layerRect = layer.bounds;
    [_camera.captureVideoPreviewLayer setBounds:layerRect];
    [_camera.captureVideoPreviewLayer setPosition:CGPointMake(CGRectGetMidX(layerRect),CGRectGetMidY(layerRect))];
    //Add to self.view's layer
    [layer addSublayer:_camera.captureVideoPreviewLayer];
    [_camera.captureSession startRunning];

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//- (IBAction)changCamerAction:(id)sender {
//    
//    [_camera changCamera];
//    
//}

- (IBAction)changeMode:(id)sender {
    UIBarButtonItem * item=sender;
    [_camera stop];
    if(_taskCode==1){
        _taskCode=2;
        [_camera setupCaptureVideoManager:RearFacingCamera];

        item.title=@"录像";
    }else{
        _taskCode=1;
        [_camera setupCaptureImageManager:RearFacingCamera];
         item.title=@"照像";

    }
    
    if(_taskCode==1){
         takeButton.type=LeafButtonTypeCamera;
        __block CameraManger *blockCamera = _camera;
        [takeButton setClickedBlock:^(LeafButton *button) {
            NSLog(@"我被点中了");
            [blockCamera captureStillImage];
        }];
        
    }else{
        
        
        takeButton.type=LeafButtonTypeVideo;
        
        __block CameraManger *blockCamera = _camera;
        [takeButton setClickedBlock:^(LeafButton *button) {
            NSLog(@" record我被点中了");
            [blockCamera  videoRecord];
        }];
        
    }
    CALayer *layer=containerView.layer;
    CGRect layerRect = layer.bounds;
    [_camera.captureVideoPreviewLayer setBounds:layerRect];
    [_camera.captureVideoPreviewLayer setPosition:CGPointMake(CGRectGetMidX(layerRect),CGRectGetMidY(layerRect))];
    //Add to self.view's layer
    [layer addSublayer:_camera.captureVideoPreviewLayer];


      [_camera.captureSession startRunning];
}


-(void)didCaptureImage:(NSData *)imageData path:(NSString *)imagePath{
    [_camera.captureSession stopRunning];
    NSLog(@"imagePath  %@",imagePath);
    _reTakeButton.hidden=NO;
    _confirmButton.hidden=NO;
    takeButton.hidden=YES;
    showImageView.hidden=NO;
    UIImage * image=[[UIImage alloc] initWithData:imageData];
   
    showImageView.image=image;
    _imagePath=imagePath;
    

}

-(void)didCaptureVideo:(NSData *)videoData path:(NSString *)videoPath{
 [_camera.captureSession stopRunning];
    NSLog(@"videoPath  %@  %d",videoPath,videoData.length);
    playerView.view.hidden=NO;
    _reTakeButton.hidden=NO;
    _confirmButton.hidden=NO;
    takeButton.hidden=YES;
    playerView.contentURL=[[NSURL alloc] initWithString:videoPath];
    _videoPath=videoPath;
      [playerView play];


}
- (IBAction)closeAction:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        [_camera.captureSession startRunning];
        _camera=nil;
    }];
    

    
    
}




- (IBAction)confirmAction:(id)sender {
    
    if (_taskCode==1) {
        [self dismissViewControllerAnimated:NO completion:^{

        }];
    }else{
    
        [self dismissViewControllerAnimated:NO completion:^{
       
            
        }];
    
    
    
    
    }
   
 
    
}
- (IBAction)reTakeAction:(id)sender {
    
    if (_taskCode==1) {
        showImageView.image=nil;
        showImageView.hidden=YES;

    }
    else{
        playerView.view.hidden=YES;
        playerView.contentURL=nil;
        
        
    
    }
      _reTakeButton.hidden=YES;
    _confirmButton.hidden=YES;
    takeButton.hidden=NO;
    [_camera.captureSession startRunning];
    
    
    
}
@end
